package com.sunny.shop.service;

import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
import com.sunny.shop.model.Song;
import com.sunny.shop.model.Theme;
import com.sunny.shop.repository.ThemeRepository;
import com.sunny.shop.vo.SongVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ThemeService {
    @Value("${sunny.oss.url}")
    private String baseUrl;
    @Autowired
    private ThemeRepository themeRepository;
    public List<SongVO> getThemeByName(String name){
        Theme result = themeRepository.findOneByName(name);
        List<Song> songRes = result.getSongList();
        Mapper mapper = DozerBeanMapperBuilder.buildDefault();
        List<SongVO> songList = new ArrayList<>();
        songRes.forEach(s->{
            String orgPlayUrl = s.getPlayUrl();
            String newPlayUrl = baseUrl + "/" + orgPlayUrl;
            String newimg = baseUrl + "/" + s.getImg();
            s.setPlayUrl(newPlayUrl);
            s.setImg(newimg);
            SongVO song = new SongVO();
            mapper.map(s, song);
            songList.add(song);
        });
        return songList;
    }
}
